package com.wuhao.lib_base.mvvm.v

import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.util.Log
import android.view.KeyEvent
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.wuhao.lib_base.R
import com.wuhao.lib_base.utils.ActivityStackManager
import com.wuhao.lib_base.utils.AndroidBugFixUtils
import com.wuhao.lib_base.utils.DialogUtil
import com.wuhao.lib_base.utils.ToastyUitl
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import java.util.concurrent.TimeUnit

/**
 * Activity基类
 *
 * @author Qu Yunshuo
 * @since 8/27/20
 */
abstract class BaseActivity<VB : ViewBinding, VM : BaseViewModel> : BaseFrameActivity<VB, VM>() {
    var newRx: CompositeDisposable = CompositeDisposable()
    var haveTouchScreen: Boolean = false

    /**
     * 设置状态栏
     * 子类需要自定义时重写该方法即可
     * @return Unit
     */
    override fun setStatusBar() {
        BarUtils.setStatusBarLightMode(this, false)//设置状态栏是否为浅色模式
        BarUtils.setStatusBarVisibility(this, true)//设置状态栏是否可见
    }

    override fun onResume() {
        super.onResume()
        //设备是否支持触控
        haveTouchScreen = packageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
        Log.d("ActivityLifecycle", "ActivityStack: ${ActivityStackManager.activityStack}")
    }

    override fun onDestroy() {
        super.onDestroy()
        // 解决某些特定机型会触发的Android本身的Bug
        if (needFixSoftInputBug) {
            AndroidBugFixUtils().fixSoftInputLeaks(this)
        }
        newRx.clear()
    }

    var needFixSoftInputBug = false

    fun showLoading(loadingName: String?) {
        showLoading(loadingName, false)
    }

    fun showLoading(loadingName: String?, isVertical: Boolean) {
        DialogUtil.showLoading(this@BaseActivity, loadingName, isVertical)
    }

    fun showLoading() {
        showLoading(getString(R.string.str_loading), false)
    }

    fun updateLoading(updateLoadingName: String?) {
        DialogUtil.updateLoadingName(updateLoadingName)
    }

    val TIME_LOADING: Long = 500

    fun dissmissLoadingNow() {
        dissmissLoading(0)
    }

    fun dissmissLoading() {
        dissmissLoading(TIME_LOADING)
    }

    fun dissmissLoading(delayTime: Long) {
        newRx.add(Observable.timer(delayTime, TimeUnit.MILLISECONDS)
            .subscribe { _: Long? ->
                DialogUtil.dismissDialog(this@BaseActivity)
            })
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    fun showError(errorName: String) {
        ToastyUitl.errorToasty(this, errorName)
    }

    fun showWarning(warningName: String) {
        ToastyUitl.warnToasty(this, warningName)
    }

    fun showSuccess(successName: String) {
        ToastyUitl.successToasty(this, successName)
    }

    /**
     * 消息提示
     *
     * @param infoName
     */
    fun showInfo(infoName: String) {
        ToastyUitl.infoToasty(this, infoName)
    }

    fun startActivity(tartgetClass: Class<*>, isFinish: Boolean) {
        val intent = Intent(this, tartgetClass)
        startActivity(intent)
        if (isFinish) {
            finish()
        }
    }

    fun startActivity(tartgetClass: Class<*>) {
        startActivity(tartgetClass, false)
    }
}